package com.example.com.skills_utf8;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class LoadMoreListViewActivity extends Activity{

	LoadMoreListViewActivity activity = this;
	ArrayList<String> listdata = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loadmore_listview);
		

		ListView lv = (ListView)findViewById(R.id.lv);
		
		listdata.add("1");
		listdata.add("2");
		listdata.add("3");
		LoadMoreAdapter adapter = new LoadMoreAdapter(activity, listdata);
		lv.setAdapter(adapter);
		
		
		// 点击一下加载更多按钮, 
		// 
		
		
		// 给listview设置监听器
		lv.setOnScrollListener( new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {  
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	
}


class LoadMoreAdapter extends BaseAdapter
{
	LoadMoreListViewActivity activity;
	ArrayList<String> data ;
	
	LayoutInflater inflater;
	
	public LoadMoreAdapter(LoadMoreListViewActivity activity, ArrayList<String> data) {
		// TODO Auto-generated constructor stub
		
		this.activity = activity;
		this.data = data;
		
		
		inflater =(LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		final ViewHolder holder;
		if(convertView == null){
			
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.adapter_loading_more, null);
			holder.LoadMore = convertView.findViewById(R.id.ViewStub_LoadMore);
			holder.layout_content =(LinearLayout)convertView.findViewById(R.id.layout_content);
			
			convertView.setTag(holder);
		}
		else{
			
			holder = (ViewHolder)convertView.getTag();
		}
		
		
		if(position == data.size()-1){ //给最后一个项
			
			
		holder.layout_content.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			//	holder.title.setVisibility(View.GONE); // 隐藏被点击的子项
				
				if(holder.LoadMore instanceof ViewStub){ //ViewStub还没有inflate
					holder.LoadMore = ((ViewStub)holder.LoadMore).inflate();
					holder.LoadMore.setVisibility(View.VISIBLE);	
				}
				else{ // 控制显示
						if(holder.LoadMore.isShown())
					//if(holder.LoadMore.getVisibility() == View.VISIBLE) //OK
						holder.LoadMore.setVisibility(View.GONE);
					else
						holder.LoadMore.setVisibility(View.VISIBLE);
				}
				
				
				
				
			}
		});
		}
	
		/*
		// 点击子项
		holder.title.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			//	holder.title.setVisibility(View.GONE); // 隐藏被点击的子项
				
				if(holder.LoadMore instanceof ViewStub){ //ViewStub还没有inflate
					holder.LoadMore = ((ViewStub)holder.LoadMore).inflate();
					holder.LoadMore.setVisibility(View.VISIBLE);	
				}
				else{ // 控制显示
					if(holder.LoadMore.getVisibility() == View.VISIBLE)
						holder.LoadMore.setVisibility(View.GONE);
					else
						holder.LoadMore.setVisibility(View.VISIBLE);
				}
				
			}
		});
		*/
		
		
		
		
		
		return convertView;
	}

	
	class ViewHolder
	{
		
		
		View LoadMore; // 延迟加载
		
		
		
		
		LinearLayout layout_content;
		TextView title;
		
	
	};
	
};